<template>
    <l-dialog :visible.sync="formVisible" :height="700" :width="1200" @close="handleCloseForm" @opened="handleOpenedForm"
        :hasBtns="true" :showMinBtn="false" :showFullscreenBtn="false" ref="lrDialog">
        <template #title>
            <h3>{{ options.isAdd ? "新建" : "编辑" }}</h3>
        </template>
        <template>
            <div class="sign-container">
                <div class="sign-word"></div>
                <h4>用地信息</h4>
            </div>
            <el-form :model="landFormData" :rules="rules" size="mini" labelPosition="top" labelWidth="110px" ref="landForm"
                class="land-form">
                <l-custom-form ref="landCustomForm" :span="6" :columns="landColumns" :formData="landFormData" />
            </el-form>
        </template>
        <template>
            <div class="sign-container">
                <div class="sign-word"></div>
                <h4>地块历年亩均税收</h4>
            </div>
            <revenueTable :landFormData="landFormData" :options="options" ref="revenueTable" />
        </template>
        <template>
            <div class="sign-container">
                <div class="sign-word"></div>
                <h4>入驻企业</h4>
            </div>
            <residentEnterpriseTable :landFormData="landFormData" :options="options" ref="residentEnterpriseTable" />
        </template>
        <template #btns>
            <!-- <el-button size="mini" @click="stageSave">暂存</el-button> -->
            <el-button size="mini" @click="handleCancel">取消</el-button>
            <el-button size="mini" type="primary" @click="handleCommit">提交</el-button>
        </template>
    </l-dialog>
</template>

<script>
import {
    landFormData,
    landColumns,
    revenueTableColmuns,
    dynApi,
} from "./columns";
import revenueTable from "./revenueTable";
import residentEnterpriseTable from "./residentEnterpriseTable";
export default {
    components: {
        revenueTable,
        residentEnterpriseTable,
    },
    props: {
        formEdit: {
            type: Boolean,
            default: true,
        },
    },
    data() {
        return {
            formVisible: false,
            landFormData,
            landColumns,
            revenueTableColmuns,
            dynApi,
            options: {},
            rules: {
                name: [{ required: true, message: "请输入内容", trigger: "blur" }],
                evaluationPrice: [{ required: false }, { pattern: /^\d+(\.\d+)?$/, message: '地块评估价格必须为数字值' }],
                coverArea: [{ required: false }, { pattern: /^\d+(\.\d+)?$/, message: '占地面积必须为数字值' }],
                existingUnfinishedLand: [{ required: false }, { pattern: /^\d+(\.\d+)?$/, message: '现有未建用地必须为数字值' }],
                originalBuildingArea: [{ required: false }, { pattern: /^\d+(\.\d+)?$/, message: '原有建筑面积必须为数字值' }],
                originalPlotRatio: [{ required: false }, { pattern: /^\d+(\.\d+)?$/, message: '原容积率必须为数字值' }],
                updatedBuildingArea: [{ required: false }, { pattern: /^\d+(\.\d+)?$/, message: '更新后建筑面积必须为数字值' }],
                updatedPlotRatio: [{ required: false }, { pattern: /^\d+(\.\d+)?$/, message: '更新后容积率必须为数字值' }],
                industrialUpdateCost: [{ required: false }, { pattern: /^\d+(\.\d+)?$/, message: '产业更新造价必须为数字值' }],
                industrialUpdateDemolitionArea: [{ required: false }, { pattern: /^\d+(\.\d+)?$/, message: '产业更新拆除面积必须为数字值' }],
                industrialUpdateNewArea: [{ required: false }, { pattern: /^\d+(\.\d+)?$/, message: '产业更新新建面积必须为数字值' }],
            },
            labelIdList: [],
        };
    },
    computed: {
        typesOptions() {
            return this.lr_dataItemOptions(this.lr_dataItem["types"]);
        },
    },
    mounted() {

    },
    methods: {
        stageSave() {
            console.log(this.$refs.landCustomForm.formData);
        },
        async open(row = {}, options = {}) {
            this.options = options;
            this.landFormData = Object.assign({}, landFormData, row);
            this.$refs.lrDialog.isfullscreen = true;
            this.$refs.lrDialog.onMousedown = function () { }
            this.formVisible = true;
            this.$nextTick(() => {
                this.$refs.landForm.clearValidate();
            });
        },
        handleCancel() {
            this.formVisible = false;
        },
        handleCommit() {
            this.$refs["landForm"].validate(async (valid) => {
                if (valid) {
                    // 存在未保存数据禁止新增或修改
                    let isNotSaved1 = this.$refs.revenueTable.tableData.length && this.$refs.revenueTable.tableData.some(i => i.isEditing === true)
                    let isNotSaved2 = this.$refs.residentEnterpriseTable.tableData.length && this.$refs.residentEnterpriseTable.tableData.some(i => i.isEditing === true)
                    if (isNotSaved1 === true || isNotSaved2 === true) {
                        this.$message({
                            type: 'error',
                            message: '当前有未保存的数据，请保存后再操作'
                        })
                        return;
                    }
                    const {
                        name,
                        id,
                        regionCode,
                        address,
                        evaluationPrice,
                        coverArea,
                        existingUnfinishedLand,
                        existingUnfinishedReason,
                        originalBuildingArea,
                        originalPlotRatio,
                        industrialUpdateCode,
                        updatedLandPropertiesCode,
                        updatedBuildingArea,
                        updatedPlotRatio,
                        landUseTypeCode,
                        industrialUpdateDemolitionArea,
                        industrialUpdateNewArea,
                        datasourceCode,
                        updateCycleCode,
                        landCertificate,
                        ownershipCertificate,
                        preUpdatePhoto,
                        updatedPhoto,
                        originalLandPropertiesCode,
                        industrialUpdateCost,
                        brief,
                        remark
                    } = this.$refs.landCustomForm.formData;
                    this.landFormData.landInfo = {
                        name,
                        id,
                        regionCode,
                        address,
                        evaluationPrice,
                        coverArea,
                        existingUnfinishedLand,
                        existingUnfinishedReason,
                        originalBuildingArea,
                        originalPlotRatio,
                        industrialUpdateCode,
                        updatedLandPropertiesCode,
                        updatedBuildingArea,
                        updatedPlotRatio,
                        landUseTypeCode,
                        industrialUpdateDemolitionArea,
                        industrialUpdateNewArea,
                        datasourceCode,
                        updateCycleCode,
                        landCertificate,
                        ownershipCertificate,
                        preUpdatePhoto,
                        updatedPhoto,
                        originalLandPropertiesCode,
                        industrialUpdateCost,
                        brief,
                        remark
                    };
                    // 税收 参数格式正确
                    this.landFormData.landMuTaxs = [...this.$refs.revenueTable.tableData]
                    // 入驻企业
                    this.landFormData.companys = [...this.$refs.residentEnterpriseTable.tableData]
                    this.landFormData.companys.forEach(i => {
                        i.landCompanyRel = {
                            occupationType: i.occupationType,
                            residence: i.residence,
                        }
                    })
                    if (this.landFormData.landInfo.id) { // 修改
                        // 参数要求格式
                        let res = await this.dynApi.update(
                            this.landFormData.landInfo.id,
                            {
                                landInfo: this.landFormData.landInfo,
                                companys: this.landFormData.companys,
                                landMuTaxs: this.landFormData.landMuTaxs
                            }
                        );
                        if (res.data.code === 200) {
                            this.$message({
                                type: "success",
                                message: "修改成功",
                            });
                            this.refresh();
                            this.formVisible = false;
                        } else {
                            this.$message({
                                type: "error",
                                message: "修改失败",
                            });
                        }
                    } else { // 新增
                        let res = await this.dynApi.add(this.landFormData);
                        if (res.data.code === 200) {
                            this.$message({
                                type: "success",
                                message: '新增成功'
                            });
                            this.refresh();
                            this.formVisible = false;
                        } else {
                            this.$message({
                                type: "error",
                                message: "新增失败"
                            })
                        }
                    }
                } else {
                    this.$message({
                        type: "error",
                        message: "您有必填项没有填写",
                    });
                    return false;
                }
            });
        },
        async handleOpenedForm(showLoading, hideLoading) {
            if (this.formEdit) {
                showLoading("加载数据中...");
                hideLoading();
            }
        },
        refresh() {
            this.$emit("refresh");
        },
        handleCloseForm() {
            this.formVisible = false;
        },
    },
};
</script>
<style lang="scss" scoped>
::v-deep .upload-files .el-upload-list.el-upload-list--text {
    width: calc(100% - 97px) !important;
}

::v-deep .el-upload-list__item.focusing.is-success .el-icon-close-tip {
    display: none !important;
}

.sign-container {
    display: flex;
    align-items: center;

    .sign-word {
        height: 14px;
        width: 4px;
        background-color: rgb(25, 118, 255);
        margin-right: 5px;
    }
}

::v-deep .l-dialog-btns {
    position: static;
}

.header {
    width: 100%;
}

::v-deep .el-dialog__body {
    padding: 10px;
}

.dialog-container {
    overflow: auto;
}

::v-deep .land-form .el-textarea textarea {
    height: 100px !important;
}
</style>
